﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.test;
using System.Media;


namespace WindowsFormsApplication1
{
    public partial class Login : Form
    {
        #region Variables

        static public bool SoundOn = true;
        static public SoundPlayer sound = new SoundPlayer(Application.StartupPath + @"\Battlefield 2 Theme.wav");
        private bool Signup = false;
        private string username;
        private string password;
        private int userID = 0;
        private Portal portal;
        private Game1 Gm;
        Service1 webservice;

        #endregion
        #region Methods

        public Login()
        {
            InitializeComponent();
            webservice = new Service1();
        }

        private void LogSignChange()
        {
            if (Signup == false)
            {
                Signup = true;
                txtConfirm.Visible = false;
                txtConfirm.Enabled = false;
                txtConfirm.Text = "";
                txtPassWord.Text = "";
                txtUserName.Text = "";
                lblConfirm.Visible = false;
                lblConfirm.Enabled = false;
                btnCancel.Location = new Point(165, 64);
                btnLogin.Location = new Point(13, 64);
                btnSignUp.Location = new Point(89, 64);
                this.Size = new Size(237, 95);
                btnCancel.Text = "Close";
                btnSignUp.Text = "Sign up";
                btnLogin.Enabled = true;
                this.Text = "Login";
            }
            else
            {
                Signup = false;
                txtConfirm.Text = "";
                txtPassWord.Text = "";
                txtUserName.Text = "";
                txtConfirm.Visible = true;
                txtConfirm.Enabled = true;
                lblConfirm.Visible = true;
                lblConfirm.Enabled = true;
                btnCancel.Location = new Point(165, 90);
                btnLogin.Location = new Point(13, 90);
                btnSignUp.Location = new Point(89, 90);
                this.Size = new Size(237, 121);
                btnCancel.Text = "Cancel";
                btnSignUp.Text = "Confirm";
                btnLogin.Enabled = false;
                this.Text = "Signup";
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            LogSignChange();
        }

        /// <summary>
        /// An instance of the Game Form.
        /// </summary>
        public void StartGame(int size, string inviter, string invitee)
        {
            Gm = new Game1(webservice.GetGameIP(), this, size, inviter, invitee, username);
            Gm.Enabled = true;
            Gm.Visible = true;
        }

        #endregion
        #region User Interface

        private void btnSignUp_Click(object sender, EventArgs e)
        {
            if (!txtConfirm.Visible)
            {
                LogSignChange();
            }
            else
            {
                DialogResult dlgResult = MessageBox.Show("Are you sure your information is correct?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlgResult == DialogResult.Yes)
                {
                    username = txtUserName.Text;
                    password = txtPassWord.Text;
                    for (int i = 0; i < username.Length; i++)
                    {
                        if (username[i] == ' ')
                        {
                            MessageBox.Show("No space allowed in your username!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    for (int i = 0; i < password.Length; i++)
                    {
                        if (password[i] == ' ')
                        {
                            MessageBox.Show("No space allowed in your password!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    if (txtConfirm.Text != password)
                    {
                        MessageBox.Show("Your passwords do not match!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        try
                        {
                            userID = webservice.Signup(username, password);
                            if (userID > 0)
                            {
                                LogSignChange();
                            }
                            else
                            {
                                MessageBox.Show("This username is already taken! \n please try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("There seems to be a problem with the server, please try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!txtConfirm.Visible)
            {
                this.Close();
            }
            else
            {
                LogSignChange();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text != "" || txtPassWord.Text != "")
            {
                username = txtUserName.Text;
                password = txtPassWord.Text;
                try
                {
                    userID = webservice.Login(username, password);
                    if (userID > 2)
                    {
                        portal = new Portal(this, username);
                        this.Visible = false;
                        portal.Enabled = true;
                        portal.Visible = true;
                    }
                    else if (userID == 0)
                    {
                        MessageBox.Show("Your username is incorrect! \n Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else if (userID == 1)
                    {
                        MessageBox.Show("Your password is incorrect! \n Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else if (userID == 2)
                    {
                        MessageBox.Show("This user is already online! \n Feel free to create your own account!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch
                {
                    MessageBox.Show("There seems to be a problem with the server, please try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #endregion
    }
}
